package com.example.demo.module.quartz;

import java.util.concurrent.TimeUnit;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;

import com.example.demo.service.SeckillService;

import lombok.extern.slf4j.Slf4j;

/**
 * 秒杀活动开始前1分钟开始监听秒杀成功用户的队列
 * @author Administrator
 *
 */
@Component
@Slf4j
public class SeckillQueueJob extends QuartzJobBean {
	@Autowired
	private RedisTemplate redisTemplate;
	@Override
	protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
		
		String productId = (String)context.getJobDetail().getJobDataMap().get("productId");
		log.info("商品-{}-秒杀活动准备开始，定时任务开始监听队列.", productId);
		String userId = (String) redisTemplate.opsForList()
		.rightPop(SeckillService.SECKILL_PRODUCT_USER_QUEUE + productId, 2, TimeUnit.MINUTES);
		log.info("用户{}成功抢到商品{}",userId,productId);
		
	}

}
